﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ESoft.AspNetCore.DTO
{
    /// <summary>
    /// 响应结果
    /// </summary>
    public class ResponseResult
    {
        /// <summary>
        /// 登录超时
        /// </summary>
        public const int LOGIN_TIME_OUT = 200;
        
        /// <summary>
        /// 应用异常
        /// </summary>
        public const int APPLICATION_ERROR = 201;
        /// <summary>
        /// 系统异常
        /// </summary>
        public const int SYSTEM_ERROR = 203;

        /// <summary>
        /// 状态码
        /// </summary>
        public int Code { get; set; } = 0;

        /// <summary>
        /// 结果
        /// </summary>
        /// <param name="v"></param>
        public ResponseResult(bool v)
        {
            this.IsSuccessed = v;
        }

        /// <summary>
        /// 结果
        /// </summary>
        /// <param name="IsSuccessed"></param>
        /// <param name="Result"></param>
        public ResponseResult(bool IsSuccessed, Object Result)
        {
            this.IsSuccessed = IsSuccessed;
            this.Result = Result;
        }

        /// <summary>
        /// 结果
        /// </summary>
        /// <param name="isSuccessed"></param>
        /// <param name="result"></param>
        /// <param name="errorMessage"></param>
        public ResponseResult(bool isSuccessed, Object result, Object errorMessage)
        {
            this.IsSuccessed = isSuccessed;
            this.Result = result;
            this.ErrorMessage = errorMessage;
        }

        /// <summary>
        /// 创建结果
        /// </summary>
        /// <param name="isSuccessed"></param>
        /// <param name="result"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        public static ResponseResult CreateResult(bool isSuccessed, Object result, Object errorMessage)
        {
            return new ResponseResult(isSuccessed, result, errorMessage);
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static ResponseResult Success(Object result)
        {
            return new ResponseResult(true, result);
        }

        /// <summary>
        /// 失败输出
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static ResponseResult FailOut(Exception result)
        {
            var res = new ResponseResult(false, result.Message, result)
            {
                Code = APPLICATION_ERROR
            };
            return res;
        }

        /// <summary>
        /// 失败输出
        /// </summary>
        /// <param name="code"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static ResponseResult FailOut(int code, Exception result)
        {
            var res = new ResponseResult(false, result.Message, result)
            {
                Code = code
            };
            return res;
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static object FailOut(string v)
        {
            return FailOut(new Exception(v));
        }
        /// <summary>
        /// 成功
        /// </summary>
        /// <returns></returns>
        public static ResponseResult Success()
        {
            return new ResponseResult(true);
        }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccessed { get; set; }

        /// <summary>
        /// 异常信息
        /// </summary>
        public Object ErrorMessage { get; set; }

        /// <summary>
        /// 结果
        /// </summary>
        public Object Result { get; set; }
    }
}
